<!DOCTYPE html>
          <head>
        <meta charset="utf-8">
            
            <title>
                「Android」让RecyclerView支持加载更多(Endless RecyclerView) | ttdevs
            </title>
            <meta content="width=device-width, initial-scale=1" name="viewport">
            <meta name="theme-color" content="#4184f3">
            
            
            <link href="/favicon.ico" rel="icon"/>
            

            <link rel="stylesheet" href="/css/highlight.light.css">
            <link rel="stylesheet" href="/css/prism-customize.css">
            <link rel="stylesheet" href="/css/nav-icon.css">
            <link rel="stylesheet" href="/css/waves.min.css">
            <link rel="stylesheet" href="/css/jquery.tocify.css">
            <link rel="stylesheet" href="/css/main.css">
            <link rel="stylesheet" href="/css/nav-indicator.css">
            
  

  
  <!-- 谷歌统计 -->
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-97465173-1', 'auto');
    ga('send', 'pageview');

  </script>
  
            </meta>
        </meta>
    </head>

    <body>
        <header>
            <!-- cover image or sth. -->
        </header>
        <div id="main" class="m-scene">
            
<div class="nav-wrapper">

    <div class="container">
        <nav>
            <div class="logo wave">
                <a href="/" id="logo">
                    ttdevs
                </a>
            </div>
            <div class="nav-toggle-icon" >
                <div class="material-hamburger">
                    <span>
                    </span>
                    <span>
                    </span>
                    <span>
                    </span>
                </div>
            </div>
            <div class="menu-wrapper">
                <div class="nav-indicator">
                </div>
                <ul class="menus">
                    
                     
                        <li>
                            <a class="wave " href="/">
                                首页
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/archives">
                                归档
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/about">
                                关于
                            </a>
                        </li>
                     
                    
                   
                </ul>
            </div>
        </nav>
    </div>
</div>
            <div class="container content">
                <div class="scene_element scene_element--fadein">
                    <div class="row">
    <div class="main">
        <article>
          
          <header class="post-header with-cover" style="background-image:url('/1970/01/01/[Android]让RecyclerView支持加载更多(Endless RecyclerView)/cover.jpg')" >
          
          </header>
          <h1 class="post-title">「Android」让RecyclerView支持加载更多(Endless RecyclerView)</h1>

          <section class="post-info">
            <span class="post-date">1970/01/01</span>
            
            <span class="post-category">
                <a class="article-category-link" href="/categories/技术/">技术</a>
            </span>
            
            
            <span class="post-tags">
              <ul class="post-tag-list"><li class="post-tag-list-item"><a class="post-tag-list-link" href="/tags/Android/">Android</a></li></ul>
            </span>
            
          </section>

          <section class="post-content">
            <h2 id="0x00-问题"><a href="/1970/01/01/[Android]让RecyclerView支持加载更多(Endless RecyclerView)/#0x00-问题" class="headerlink" title="0x00 问题"></a>0x00 问题</h2><p>好久没有写博客，最近正好有时间，打算把之前很长很长一段时间落下的坑都给填了。</p>
<p>在Android的Support包中我们有很多新的组件可以使用，比如代替ListView的RecyclerView。使用RecyclerView和SwipeRefreshLayout组合，我们可以实现很酷炫的下拉刷新的功能。但是，事情总不是那么美好，有了下拉刷新，却找不到上拉加载更多（我们已经习惯了PullToRefresh），最终还是放弃了使用RecyclerView。现在看来，自己实在是太懒了。好了，今天就让我们的RecyclerView支持加载更多。</p>
<h2 id="0x01-Endless-RecyclerView"><a href="/1970/01/01/[Android]让RecyclerView支持加载更多(Endless RecyclerView)/#0x01-Endless-RecyclerView" class="headerlink" title="0x01 Endless RecyclerView"></a>0x01 Endless RecyclerView</h2><p>RecyclerView的的使用多数是与SwipeRefreshLayout进行组合，当然，你也可以算上CardView。使用SwipeRefreshLayout时，受PullToRefresh的影响，很自然的会想到给SwipeRefreshLayout加个上拉加载更多的功能。如果你也这么想，那么我们就一起来YY下google为什么没有官方支持这么做。</p>
<ul>
<li>给SwipeRefreshLayout添加加载更多功能：我们期望的(比如：SwipeRefreshLayout + RecyclerView，但是google没这么做)。如果SwipeRefreshLayout中是TextView或者自定义的View，很显然，我们并不能保证这个加载更多是可以实现的（即使实现了，他们耦合也会很高）。</li>
<li>给RecyclerView添加加载更多功能：这个方案是可行的</li>
<li>给其他更多SwipeRefreshLayout包含的view添加加载更多功能：第二种情况的扩展，这个根据具体的view来讨论</li>
</ul>
<p>因此，我们应该把加载更多放到具体的view中。</p>
<h2 id="0x02-实现"><a href="/1970/01/01/[Android]让RecyclerView支持加载更多(Endless RecyclerView)/#0x02-实现" class="headerlink" title="0x02 实现"></a>0x02 实现</h2><p>下面是我们支持加载更多的代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/*</span></div><div class="line">     * Created by ttdevs 9/3/15 1:31 PM. https://github.com/ttdevs</div><div class="line">     * Copyright (c) 2015 ttdevs, Inc. All rights reserved.</div><div class="line">     */</div><div class="line"></div><div class="line"><span class="keyword">package</span> com.ttdevs.support.recycler;</div><div class="line"></div><div class="line"><span class="keyword">import</span> android.support.v7.widget.LinearLayoutManager;</div><div class="line"><span class="keyword">import</span> android.support.v7.widget.RecyclerView;</div><div class="line"></div><div class="line"><span class="keyword">import</span> com.ttdevs.utils.LogUtils;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * RecyclerView加载更多的类,支持LinearLayoutManager和GridLayoutManager</div><div class="line"> * 使用:</div><div class="line"> * RecyclerView.addOnScrollListener(new OnRecyclerLoadMoreListener()&#123;</div><div class="line"> * public void onLoadMore()&#123;...&#125;</div><div class="line"> * &#125;);</div><div class="line"> */</div><div class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">OnRecyclerLoadMoreListener</span> <span class="keyword">extends</span> <span class="title">RecyclerView</span>.<span class="title">OnScrollListener</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> LinearLayoutManager mLayoutManager;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">int</span> mItemCount, mLastCompletely, mLastLoad;</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line">     * load more</div><div class="line">     */</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">onLoadMore</span><span class="params">()</span></span>;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onScrolled</span><span class="params">(RecyclerView recyclerView, <span class="keyword">int</span> dx, <span class="keyword">int</span> dy)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (recyclerView.getLayoutManager() <span class="keyword">instanceof</span> LinearLayoutManager) &#123;</div><div class="line">            mLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();</div><div class="line"></div><div class="line">            mItemCount = mLayoutManager.getItemCount();</div><div class="line">            mLastCompletely = mLayoutManager.findLastCompletelyVisibleItemPosition();</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            <span class="keyword">return</span>;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">if</span> (mLastLoad != mItemCount &amp;&amp; mItemCount == mLastCompletely + <span class="number">1</span>) &#123;</div><div class="line">            LogUtils.debug(String.format(<span class="string">"****************load more****************"</span>));</div><div class="line">            LogUtils.debug(String.format(<span class="string">"mItemCount:%d \t mLastCompletely:%d"</span>, mItemCount, mLastCompletely));</div><div class="line">            LogUtils.debug(String.format(<span class="string">"****************load more****************"</span>));</div><div class="line"></div><div class="line">            mLastLoad = mItemCount;</div><div class="line">            onLoadMore();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><img src="https://raw.githubusercontent.com/ttdevs/ttdevs.github.io/common/images/logo.png" alt="Create by ttdevs"></p>

          </section>
        </article>
        

       
        <div class="pager">
          
            <a class="post-prev pager-item" href="/1970/01/01/[Flask]0x02给我们的Flask应用配置Supervisor/" >
              <strong class="article-nav-caption">上一篇</strong>
              <p class="post-nav-title">「Flask」0x02给我们的Flask应用配置Supervisor</p>
            </a>
          
          
            <a class="post-next pager-item" href="/1970/01/01/[Python]文件拷贝工具Shutil介绍/">
              <strong class="article-nav-caption">下一篇</strong>
              <p class="post-nav-title">「Python」文件拷贝工具Shutil介绍</p>
            </a>
          
        </div>
        

         <!-- comments -->
        <div class="comment-section">
  
    


</div>

    </div>
    
    <aside>
        <div id="toc">
        </div>
    </aside>
    
</div>

                </div>
            </div>
        </div>
        <footer class="footer">
    <p>由<a href="http://hexo.io/" target="_blank">Hexo</a>强力驱动，搭载<a href="https://github.com/wayou/hexo-theme-gstyle">gstyle</a>主题</p>
    <p>
        &copy; 2017 ttdevs
    </p>
</footer>
<script src="/lib/jquery.js"></script>
<script src="/lib/waves.js"></script>
<script src="/lib/jquery-ui.js"></script>
<script src="/lib/jquery.tocify.js"></script>
<script src="/js/main.js"></script>

    </body>
</html>
